import 'whatwg-fetch';
import qs from 'qs';
const HOST = 'http://localhost:8086/';
const _request = async (method = 'GET', url, data = {}, headers = {}) => {
  method = method.toUpperCase();
  if (method === 'GET') {
    if (Object.keys(data).length > 0) {
      url = url + '?' + qs.stringify(data);
    }
    data = undefined;
  }
  const fetchConfig = {
    method: method,
    headers,
    body: data ? JSON.stringify(data) : undefined
  };
  const _url = (url.indexOf('http://') === 0 || url.indexOf('https://') === 0) ? url : `${HOST}${url}`;
  const response = await fetch(_url, fetchConfig);
  return response.json();
};

export function request(method, url, data) {
  return _request(method, url, data);
}

export function noAuthRequest(method, url, data) {
  return _request(method, url, data);
}
